import cv2
import os
from moviepy import VideoFileClip
filenames=os.listdir("处理前")
video =os.path.join("处理前",filenames[0])
clip = VideoFileClip(video)
duration = clip.duration
segments = []
segment_length=600  #时长
new_folder=filenames[0].split(".")[0]
if not os.path.exists(os.path.join("处理后",new_folder)):
      os.makedirs(os.path.join("处理后",new_folder))
a=1
for i in range(0, int(duration), segment_length):
    segment = clip.subclipped(i, i + segment_length)
    new_filename=new_folder+"_"+str(a)+".mp4"
    segment_path=os.path.join("处理后/"+new_folder,new_filename)
    #segment_path = f"segments/segment_{i}.mp4"
    segment.write_videofile(segment_path)
    segments.append(segment_path)
    a=a+1
# TOTAL_FRAME = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))  # 获取视频总帧数
# FPS = cap.get(cv2.CAP_PROP_FPS)# 获取视频总帧率
# sum_duration = TOTAL_FRAME / FPS #总时长
# size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# print(size)
# END_TIME=0
# TIME=120
# i=0
# COUNT = 0
# while END_TIME<sum_duration:
#     print(END_TIME)
#     i=i+1
#     START_TIME= TIME*(i-1)
#     END_TIME= TIME*i
   
#     if END_TIME>sum_duration:
#         END_TIME=sum_duration
#     print(START_TIME,END_TIME)
#     frameToStart = START_TIME * FPS  # 开始帧 = 开始时间*帧率
#     print(frameToStart)
#     frametoStop = END_TIME * FPS  # 结束帧 = 结束时间*帧率
#     print(frametoStop)
#     new_folder=filenames[0].split(".")[0]
#     if not os.path.exists(os.path.join("处理后",new_folder)):
#         os.makedirs(os.path.join("处理后",new_folder))
#     new_filename=new_folder+"_"+str(i)+".mp4"
#     videoWriter = cv2.VideoWriter(
#         os.path.join("处理后/"+new_folder,new_filename), cv2.VideoWriter_fourcc(*'mp4v'), FPS, size)
#     cap.set(cv2.CAP_PROP_POS_FRAMES, frameToStart)  # 设置读取的位置,从第几帧开始读取视频
   
    
    # while True:
    #     success, frame = cap.read()
    #     if success:
    #         COUNT += 1
    #         if COUNT <= frametoStop and COUNT > frameToStart:  # 选取起始帧
    #             #print('correct= ', COUNT)
    #             videoWriter.write(frame)
    #     # print('mistake= ', COUNT)
    #     if COUNT>= frametoStop:
    #         break
    # print('end')
print("结束")